#include "LED.h"
#include"KEY.h"
#include <string.h>
#include "PWM.h"
#include "UART.h"

Typedef_LED LedCtrl = {0};
void    initLED(void)
{
    DIS_LED1();
}

void LED_Set(uint8 buf, uint16 tm_on, uint16 tm_period, uint8 loop,void(*FuncTimerCallBack)(void))
{
    LedCtrl.Cnt = 0;
    LedCtrl.Buf = buf;
    LedCtrl.TmOn = tm_on>>3;
    LedCtrl.TmPeriod = tm_period>>3;
    LedCtrl.Loop = loop;
    LedCtrl.FuncCallBack = FuncTimerCallBack;
}


uint8 LedBuf=0;

void LED_Main(void)
{
    if(LedCtrl.Loop)
    {
        LedCtrl.Cnt++;
        if(LedCtrl.Cnt > LedCtrl.TmPeriod)
        {
            LedCtrl.Cnt = 0;
            if(LedCtrl.Loop != INFINITE)
            {
                LedCtrl.Loop--;
                if((LedCtrl.Loop == 0) && (LedCtrl.FuncCallBack != NULL))
                {
                    LedCtrl.FuncCallBack();
                }
            }
        }
        
        if((LedCtrl.Cnt >= LedCtrl.TmOn) || (LedCtrl.Loop==0))
        {
            CLOSE_LED();
        }
        else 
        {
            OPEN_LED();
        }
    }
    else
    {
        CLOSE_LED();
    }
}

uint8 LED_Check(void)
{
    return LedCtrl.Loop;
}
